package com.cxq.finalproject.fragment;

import static android.content.Context.MODE_PRIVATE;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import com.cxq.finalproject.Upload_Img_Activity;
import com.cxq.finalproject.databinding.FragmentMeBinding;
import com.cxq.finalproject.util.GetImgIDByName;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.cxq.finalproject.Update_userInfo;
import com.cxq.finalproject.util.MyUserHelper;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FragmentMe#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FragmentMe extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private FragmentMeBinding binding;
    private SharedPreferences sp;
    private MyUserHelper myUserHelper;
    private GetImgIDByName getImgID;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public FragmentMe() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FragmentMe.
     */
    // TODO: Rename and change types and number of parameters
    public static FragmentMe newInstance(String param1, String param2) {
        FragmentMe fragment = new FragmentMe();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
    //onCreate类似于Activity.onCreate，在其中可初始化除了view之外的一切；
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    //onCreateView是创建该fragment对应的视图，其中需要创建自己的视图并返回给调用者；
    //该方法返回视图文件，相当于Activity中onCreate方法中setContentView一样
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //bingding初始化
        binding = FragmentMeBinding.inflate(inflater);
        //sharepreference初始化
        sp = getActivity().getSharedPreferences("Login_UserInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        //数据库初始化
        myUserHelper = new MyUserHelper(getActivity(), MyUserHelper.DB_NAME, null, 1);
//        binding.imageView.setImageResource(R.drawable.user_default);
//        binding.imageView.setImageResource(R.drawable.user_default);
        getImgID = new GetImgIDByName("user_default");
        binding.imageView.setImageResource(getImgID.getResId());
        String userName = getUserName();
        binding.textView.setText(userName);
        editor.putString("userName",userName);
        editor.commit();
        //监听修改信息按钮
        binding.button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Update_userInfo.class);
                startActivity(intent);
            }
        });
        //监听钱包按钮
        binding.button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(),"正在开发中",Toast.LENGTH_SHORT).show();
            }
        });
        //监听上传按钮
        binding.button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Upload_Img_Activity.class);
                startActivity(intent);
            }
        });
        return binding.getRoot();
    }

    public String getUserName(){
        String userId = sp.getString("userId","");
        SQLiteDatabase db = myUserHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        Cursor c = db.query(MyUserHelper.TABLE_NAME, new String[] {MyUserHelper.UserNAME,},
                MyUserHelper.ID + "=? and " + MyUserHelper.PWD + "=?",
                new String[] { sp.getString("userId",""), sp.getString("password","") }, null, null, null);
        String userName = "";
        if(c.moveToFirst()){
            do{
                userName = c.getString(0);
            }while(c.moveToNext());
        }
        c.close();
        return userName;
    }
}